import 'package:flutter_app/base/base_presenter.dart';
import 'package:flutter_app/base/i_view.dart';
import 'package:flutter_app/constant.dart';
import 'package:flutter_app/http/http_manager.dart';
import 'package:flutter_app/model/login_req.dart';
import 'package:flutter_app/model/login_resp.dart';
import 'package:flutter_app/utils/log_util.dart';
import 'package:oktoast/oktoast.dart';
import 'package:sp_util/sp_util.dart';

class LoginPresenter extends BasePresenter<ILoginView> {
  LoginPresenter(ILoginView view) : super(view);

  void login(String name, String pwd) {
    LogUtil.v('$name  $pwd');
    LoginRequest request = LoginRequest(username: name, password: pwd);
    var map = request.toJson();
    HttpManager().post(
        params: map,
        url: Constant.API_LOGIN,
        tag: Constant.API_LOGIN,
        successCallback: (value) {
          LogUtil.v('------------>${value.toString()}');
          var loginResp = LoginResp.fromJson(value);
          LogUtil.v('============>${loginResp.id.toString()}');
          SpUtil.putString(Constant.USER_ID, loginResp.id.toString());
          SpUtil.putString(Constant.USER_TOKEN, loginResp.token.toString());
          SpUtil.putString(Constant.USER_NAME, loginResp.nickname.toString());
          view.loginSuccess();
        },
        errorCallback: (error) {
          showToast(error.message);
        });
  }
}

abstract class ILoginView extends IView {
  void loginSuccess();
}
